package syren;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
/**
 * Main UI setup.
 * @author Chris, Tim
 *
 */
public class mainWindow {

	JFrame frame;
	JTabbedPane menuContainer;
	String name, path = "";
	boolean openDomainTab = false;
	Domain domain;

	public mainWindow(){
		frame = new JFrame("Syren");
		menuContainer = new JTabbedPane();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void create(){
		JMenuItem create;
		JMenuItem load;

		//Menu bar containing Domain and Configuration drop down menus
		JMenuBar topBar = new JMenuBar();

		//Domain drop down menu
		JMenu topMenu = new JMenu("Domain");

		//Create + action listener
		create = new JMenuItem("Create Domain");
		create.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) 
			{
				if(openDomainTab == false){
					domain = new Domain();
					Container domainPanel = domain.create();
					String domainDisplayName = domain.getDisplayName();
					createTab(domainDisplayName, domainPanel, true);
					openDomainTab=true;
				}
			}
		});

		//Load + action listener
		load = new JMenuItem("Load Domain");
		load.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) 
			{
				if(openDomainTab == false){
					JFileChooser chooser=new  JFileChooser();
					int returnVal = JFileChooser.CANCEL_OPTION;
					Container domainPanel=null;
					do {
						String filePath;
						returnVal = chooser.showOpenDialog(null);
						if(returnVal == JFileChooser.APPROVE_OPTION) {
							File f = chooser.getSelectedFile();
							filePath = f.getAbsolutePath();
							domain = new Domain();
							domainPanel = domain.load(filePath);
							if(domainPanel==null)
								JOptionPane.showMessageDialog(null, "This is not a valid Domain Definition XML file. Please select another file.");
						}
						else
							return;
					} while (domainPanel==null && returnVal != JFileChooser.CANCEL_OPTION);
					
					String domainDisplayName = domain.getDisplayName();
					createTab(domainDisplayName, domainPanel, true);
					openDomainTab=true;
				}
				

			}
		});


		//Adding create and load to topMenu 
		topMenu.add(create);
		topMenu.add(load);

		//adding top menu to topBar
		topBar.add(topMenu);

		//Configuration drop down menu 
		topMenu = new JMenu ("Configuration");
		//create + action listener
		create = new JMenuItem("Create Configuration");
		create.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) 
			{
				menuContainer.addTab("Create Tab", new Configuration(1));
			}
		});
		//load + action listener
		load = new JMenuItem("Load Configuration");
		load.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) 
			{
				menuContainer.addTab("Load Tab", new Configuration(0));

			}
		});
		//adding both to topMenu
		topMenu.add(create);
		topMenu.add(load);
		//adding this to topBar
		topBar.add(topMenu);

		frame.getContentPane().add(topBar, BorderLayout.NORTH);


		menuContainer.setPreferredSize(new Dimension(750, 310));
		frame.getContentPane().add(menuContainer,BorderLayout.CENTER);

		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	/**
	 * This method is called by any button that creates a tab
	 * in the main window and creates save and exit button for each tab
	 *  
	 * @param displayName string value to be displayed on new tab being made
	 * @param centerPanel
	 */
	public void createTab(String displayName, Container centerPanel, boolean isDomain){
		final boolean isADomain = isDomain;
		final JPanel tabContents = new JPanel();
		tabContents.setLayout(new BorderLayout());
		JPanel bottomDomainTab = new JPanel();
		bottomDomainTab.setLayout(new GridLayout(1,3));

		JButton save = new JButton("Save");
		JButton saveAndExit = new JButton("Save As"); //Add action listener to call save method
		JButton exit = new JButton("Close Current File");

		bottomDomainTab.add(save);
		bottomDomainTab.add(saveAndExit);
		bottomDomainTab.add(exit);

		tabContents.add(bottomDomainTab, BorderLayout.SOUTH);

		/**
		 * exits the current tab and resets domainTab variable if necessary
		 */
		exit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) 
			{
				if(isADomain){						//resets openDomainTab only if the tab you are closing is a domain 
					openDomainTab=false;
				}
				menuContainer.remove(tabContents);
				path="";
			}
		});

		/**
		 * save as dialog box appears and allows to overwrite only if approved of
		 */
		saveAndExit.addActionListener(new ActionListener(){
			boolean success;
			public void actionPerformed(ActionEvent arg0){
				@SuppressWarnings("serial")
				JFileChooser fc = new JFileChooser(){
					@Override
					public void approveSelection(){
						File f = getSelectedFile();
						if(f.exists() && getDialogType() == SAVE_DIALOG){
							int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_CANCEL_OPTION);
							switch(result){
							case JOptionPane.YES_OPTION:
								super.approveSelection();
								return;
							case JOptionPane.NO_OPTION:
								return;
							case JOptionPane.CANCEL_OPTION:
								cancelSelection();
								return;
							}
						}
						super.approveSelection();
					}
				};
				File file;
				
					int returnVal = fc.showSaveDialog(null);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						path = fc.getSelectedFile().getAbsolutePath();
					}
					else{
						return;
					}
				
				if(path.endsWith(".xml"))
					file = new File(path);
				else
					file = new File(path + ".xml");

				//the tab you are closing is a domain 
				if(isADomain){
					success = domain.writeDomainXML(file);
				}
				//the tab you are closing is a config 
				else{
					//success = config.save(file);
				}

				

			}
		});


		/**
		 * save as dialog box appears if save has not already occured
		 *  and allows to overwrite only if approved of
		 */
		save.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				JFileChooser fc = new JFileChooser(){
					@Override
					public void approveSelection(){
						File f = getSelectedFile();
						if(f.exists() && getDialogType() == SAVE_DIALOG){
							int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_CANCEL_OPTION);
							switch(result){
							case JOptionPane.YES_OPTION:
								super.approveSelection();
								return;
							case JOptionPane.NO_OPTION:
								return;
							case JOptionPane.CANCEL_OPTION:
								cancelSelection();
								return;
							}
						}
						super.approveSelection();
					}
				};

				File file;
				if (path.isEmpty()){
					int returnVal = fc.showSaveDialog(null);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						path = fc.getSelectedFile().getAbsolutePath();
					}
					else{
						return;
					}
				}
				if(path.endsWith(".xml"))
					file = new File(path);
				else
					file = new File(path + ".xml");

				//the tab you are closing is a domain 
				if(isADomain){
					domain.writeDomainXML(file);
				}
				//the tab you are closing is a config 
				else{
					//success = config.save(file);
				}
			}
		});

		tabContents.add(centerPanel, BorderLayout.NORTH);
		menuContainer.addTab(displayName, tabContents);
	}
}

